﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Objects.DataClasses;
using System.Data.Linq.Mapping;

namespace TagFlo
{
    public partial class PhotoDBEntities 
    {

        public static Dictionary<Type, int> _primaryKeysCache = new Dictionary<Type, int>();
/*        public static int GenerateID<TEntity>(
            Func<PhotoDBEntities, ObjectQuery<TEntity>> table,
            Func<TEntity, int> idSelector) where TEntity : EntityObject
        {
            lock (_primaryKeysCache)
            {
                int primaryKey = 0;
                if (!_primaryKeysCache.TryGetValue(typeof(TEntity), out primaryKey))
                {
                    using (PhotoDBEntities model = PhotoDBEntities.CreateContext())
                    {
                        primaryKey = table(model).Any() ? table(model).Max(idSelector) : 0;
                    }
                }
                primaryKey++;
                _primaryKeysCache[typeof(TEntity)] = primaryKey;
                return primaryKey;
            }
        }

  */      


        public static bool ShouldGenerateID = true;
        public static PhotoDBEntities CreateContext()
        {
            return new PhotoDBEntities();
        }
    }

    public partial class Photo
    {
        public static Photo New()
        {
            Photo ret = new Photo();

            if (PhotoDBEntities.ShouldGenerateID)
            {
                lock (PhotoDBEntities._primaryKeysCache)
                {
                    int primaryKey = 0;
                    if (!PhotoDBEntities._primaryKeysCache.TryGetValue(typeof(Photo), out primaryKey))
                    {
                        using (PhotoDBEntities model = new PhotoDBEntities())
                        {
                            primaryKey = model.Photos.Any() ? model.Photos.Max(p => p.PhotoID) : 0;
                        }
                    }
                    primaryKey++;
                    PhotoDBEntities._primaryKeysCache[typeof(Photo)] = primaryKey;
                    ret.PhotoID = primaryKey;
                }
            }
                //ret.PhotoID = PhotoDBEntities.GenerateID(m => m.Photos,  p => p.PhotoID );
            return ret;
        }
    }

    public partial class PhotoSet
    {
        public static PhotoSet New()
        {
            PhotoSet ret = new PhotoSet();

            if (PhotoDBEntities.ShouldGenerateID)
            {
                lock (PhotoDBEntities._primaryKeysCache)
                {
                    int primaryKey = 0;
                    if (!PhotoDBEntities._primaryKeysCache.TryGetValue(typeof(PhotoSet), out primaryKey))
                    {
                        using (PhotoDBEntities model = new PhotoDBEntities())
                        {
                            primaryKey = model.PhotoSets.Any() ? model.PhotoSets.Max(p => p.PhotoSetID) : 0;
                        }
                    }
                    primaryKey++;
                    PhotoDBEntities._primaryKeysCache[typeof(PhotoSet)] = primaryKey;
                    ret.PhotoSetID = primaryKey;
                }
            }
            //ret.PhotoID = PhotoDBEntities.GenerateID(m => m.Photos,  p => p.PhotoID );
            return ret;
        }
    }
    public partial class SmartSet
    {
        public static SmartSet New()
        {
            SmartSet ret = new SmartSet();

            if (PhotoDBEntities.ShouldGenerateID)
            {
                lock (PhotoDBEntities._primaryKeysCache)
                {
                    int primaryKey = 0;
                    if (!PhotoDBEntities._primaryKeysCache.TryGetValue(typeof(SmartSet), out primaryKey))
                    {
                        using (PhotoDBEntities model = new PhotoDBEntities())
                        {
                            primaryKey = model.SmartSets.Any() ? model.SmartSets.Max(p => p.SmartSetID) : 0;
                        }
                    }
                    primaryKey++;
                    PhotoDBEntities._primaryKeysCache[typeof(SmartSet)] = primaryKey;
                    ret.SmartSetID = primaryKey;
                }
            }
            //ret.PhotoID = PhotoDBEntities.GenerateID(m => m.Photos,  p => p.PhotoID );
            return ret;
        }
    }

    public partial class SmartSetFilter
    {
        public static SmartSetFilter New()
        {
            SmartSetFilter ret = new SmartSetFilter();

            if (PhotoDBEntities.ShouldGenerateID)
            {
                lock (PhotoDBEntities._primaryKeysCache)
                {
                    int primaryKey = 0;
                    if (!PhotoDBEntities._primaryKeysCache.TryGetValue(typeof(SmartSetFilter), out primaryKey))
                    {
                        using (PhotoDBEntities model = new PhotoDBEntities())
                        {
                            primaryKey = model.SmartSetFilters.Any() ? model.SmartSetFilters.Max(p => p.SmartSetFilterID) : 0;
                        }
                    }
                    primaryKey++;
                    PhotoDBEntities._primaryKeysCache[typeof(SmartSetFilter)] = primaryKey;
                    ret.SmartSetFilterID = primaryKey;
                }
            }
            //ret.PhotoID = PhotoDBEntities.GenerateID(m => m.Photos,  p => p.PhotoID );
            return ret;
        }
    }
 
    
    public partial class Path
    {
        public static Path New()
        {
            Path ret = new Path();

            if (PhotoDBEntities.ShouldGenerateID)
            {
                lock (PhotoDBEntities._primaryKeysCache)
                {
                    int primaryKey = 0;
                    if (!PhotoDBEntities._primaryKeysCache.TryGetValue(typeof(Path), out primaryKey))
                    {
                        using (PhotoDBEntities model = new PhotoDBEntities())
                        {
                            primaryKey = model.Paths.Any() ? model.Paths.Max(p => p.PathID) : 0;
                        }
                    }
                    primaryKey++;
                    PhotoDBEntities._primaryKeysCache[typeof(Path)] = primaryKey;
                    ret.PathID = primaryKey;
                }
            }
            //ret.PhotoID = PhotoDBEntities.GenerateID(m => m.Photos,  p => p.PhotoID );
            return ret;
        }
    }
    public partial class Tag
    {
        public static Tag New()
        {
            Tag ret = new Tag();

            if (PhotoDBEntities.ShouldGenerateID)
            {
                lock (PhotoDBEntities._primaryKeysCache)
                {
                    int primaryKey = 0;
                    if (!PhotoDBEntities._primaryKeysCache.TryGetValue(typeof(Tag), out primaryKey))
                    {
                        using (PhotoDBEntities model = new PhotoDBEntities())
                        {
                            primaryKey = model.Tags.Any() ? model.Tags.Max(p => p.TagID) : 0;
                        }
                    }
                    primaryKey++;
                    PhotoDBEntities._primaryKeysCache[typeof(Tag)] = primaryKey;
                    ret.TagID = primaryKey;
                }
            }
            //ret.PhotoID = PhotoDBEntities.GenerateID(m => m.Photos,  p => p.PhotoID );
            return ret;
        }
    }

    public partial class TagSet
    {
        public static TagSet New()
        {
            TagSet ret = new TagSet();

            if (PhotoDBEntities.ShouldGenerateID)
            {
                lock (PhotoDBEntities._primaryKeysCache)
                {
                    int primaryKey = 0;
                    if (!PhotoDBEntities._primaryKeysCache.TryGetValue(typeof(TagSet), out primaryKey))
                    {
                        using (PhotoDBEntities model = new PhotoDBEntities())
                        {
                            primaryKey = model.TagSets.Any() ? model.TagSets.Max(p => p.TagSetID) : 0;
                        }
                    }
                    primaryKey++;
                    PhotoDBEntities._primaryKeysCache[typeof(TagSet)] = primaryKey;
                    ret.TagSetID = primaryKey;
                }
            }
            //ret.PhotoID = PhotoDBEntities.GenerateID(m => m.Photos,  p => p.PhotoID );
            return ret;
        }
    }

}
